package com.itany.corejava.day16;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestJProGressBar {

    private JFrame jf;
    private JProgressBar jpb;
    private JButton jb;

    public TestJProGressBar() {
        jf=new JFrame();
        jpb=new JProgressBar();
        jb=new JButton("start");
    }

    private void init(){
        jf.setLayout(new FlowLayout());
        jf.add(jpb);
        jf.add(jb);
    }

    private void setStyle(){
        jpb.setStringPainted(true);
    }

    private void addEventHandler(){
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Thread(){
                    @Override
                    public void run() {
                        for (int i = 0; i <= 100; i++) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e1) {
                                e1.printStackTrace();
                            }
                            jpb.setValue(i);
                        }
                    }
                }.start();

            }
        });
    }

    public void show(){
        init();
        setStyle();
        addEventHandler();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestJProGressBar().show();
    }



}
